import request from '@/config/axios'
import type { Dayjs } from 'dayjs'

/** 模具保养任务信息 */
export interface UpkeepTask {
  id: number // 主键
  code?: string // 保养单号
  drawingNoId?: number // 图号id
  accountId?: number // 模具id
  assigneerId?: number // 指派人Id
  upkeepId: number // 保养类型id
  status: number // 任务状态
  nextUpkeepTime: string | Dayjs // 计划保养日期
  realUpkeepTime: string | Dayjs // 实际保养日期
  taskCreateTime: string | Dayjs // 任务下达时间
  taskReceiveTime: string | Dayjs // 任务接收时间
  jobStartTime?: string | Dayjs // 作业开始时间
  taskStartTime?: string | Dayjs // 任务开始时间
  taskEndTime?: string | Dayjs // 任务结束时间
  assigneerName?: string // 接收人名称
}

export interface UpkeepTaskModStautus {
  id: number // 主键
  status: number // 任务状态
  data?: string // 参数
}

export interface UpkeepTaskWorkReport {
  id: number
  assigneerId: string
  taskReceiveTime: string
  jobStartTime: string
  taskStartTime: string
  taskEndTime: string
  taskExecutionDuration: number
  assigneerIds: number[]
  description: string
  isCompleted: boolean
  upkeepItems: {
    taskId: number
    itemId: number
  }[]
}

// 模具保养任务 API
export const UpkeepTaskApi = {
  // 查询模具保养任务分页
  getUpkeepTaskPage: async (params: any) => {
    return await request.get({ url: `/mold/upkeep-task/page`, params })
  },

  // 查询模具保养任务详情
  getUpkeepTask: async (id: number) => {
    return await request.get({ url: `/mold/upkeep-task/get?id=` + id })
  },

  // 新增模具保养任务
  createUpkeepTask: async (data: UpkeepTask) => {
    return await request.post({ url: `/mold/upkeep-task/create`, data })
  },

  // 修改模具保养任务
  updateUpkeepTask: async (data: UpkeepTask) => {
    return await request.put({ url: `/mold/upkeep-task/update`, data })
  },

  // 删除模具保养任务
  deleteUpkeepTask: async (id: number) => {
    return await request.delete({ url: `/mold/upkeep-task/delete?id=` + id })
  },

  /** 批量删除模具保养任务 */
  deleteUpkeepTaskList: async (ids: number[]) => {
    return await request.delete({ url: `/mold/upkeep-task/delete-list?ids=${ids.join(',')}` })
  },

  // 导出模具保养任务 Excel
  exportUpkeepTask: async (params) => {
    return await request.download({ url: `/mold/upkeep-task/export-excel`, params })
  },

  // 修改模具保养任务状态
  modStatus: async (data: UpkeepTaskModStautus) => {
    return await request.put({ url: `/mold/upkeep-task/mod-status`, data })
  },

  // 提交工作报表
  submitWorkReport: async (data: UpkeepTaskWorkReport) => {
    return await request.put({ url: `/mold/upkeep-task/work-report`, data })
  }
}
